import time
from selenium.webdriver.support.select import Select

from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
class Base():
    def __init__(self,driver):
        self.driver=driver

    #打开网址
    def open_url(self,url):
        self.driver.get(url)

    #截屏时间报错
    def time_jirping(self):
        jp=time.strftime("%Y%m%d%H%M%S",time.localtime())
        self.driver.save_screenshot("../Png" + "Img" + jp + ".png")

    #定位元素
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,10,0.5).until(lambda driver:driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            print(e)
            self.time_jirping()

    #文本
    def text_wenben(self,loc):
        return self.driver.find_element(loc).text

    #输入内容
    def send_keys(self,loc,info):
        self.driver.find_element(loc).send_keys(info)

    # #滚动条
    # def scorllTo(self):

    #弹窗处理
    def alert_accept(self):
        self.driver.swich_to.alert.accept()

    #iframe
    def Iframe(self,loc):
        self.driver.swich_to.frame(self.find_element(loc))

    #切换页面
    def new_window(self):
        self.driver.swich_to.window(self.driver.window_handles[-1])

    #鼠标事件
    def double_click(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).perform()
    #键盘事件
    def ENTER(self,loc):
        self.find_element(loc).send_keys(Keys.ENTER)

    # #浏览器事件
    # def browser_liu(self):

    #下拉框
    def select(self,loc,x):
        value=Select(self.find_element(loc))
        value.select_by_value(x)

    #鼠标点击
    def dianji_click(self,loc):
        self.find_element(loc).click()